package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.ComplexAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.SpRevokeProbationConvert;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditActionResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpRevokeProbationCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.SpRevokeProbationQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpRevokeProbationResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpComplexPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpRevokeProbationUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpRevokeProbationEntity;
import com.engine4cloud.tdf.supervision.mapper.SpRevokeProbationMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.SpAuditResultService;
import com.engine4cloud.tdf.supervision.service.SpRevokeProbationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 提请撤销缓刑
 *
 * @author wenjie
 * @date 2024-04-25 09:39:53
 */
@Service
public class SpRevokeProbationServiceImpl extends ServiceImpl<SpRevokeProbationMapper, SpRevokeProbationEntity> implements SpRevokeProbationService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private SpAuditResultService auditResultService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<SpRevokeProbationResultDTO> getPageList(Params pageable, SpRevokeProbationQueryDTO dto) {
        IPage<SpRevokeProbationResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public SpRevokeProbationResultDTO getById(Long id) {
        SpRevokeProbationResultDTO result = Optional.ofNullable(SpRevokeProbationConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    @Override
    public SpRevokeProbationResultDTO getByBatch(String batch) {
        return SpRevokeProbationConvert.INSTANCE.convert(super.getOne(wrapper(new SpRevokeProbationQueryDTO(batch))));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(SpRevokeProbationCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        //保存主表信息
        SpRevokeProbationEntity entity = SpRevokeProbationConvert.INSTANCE.create(dto, dx.getTenantId(), ComplexAuditActionStatusEnum.CREATE);
        super.save(entity);
        //保存文件信息
        this.saveInstrumentList(dto, entity, user);
        //保存审核信息
        return auditResultService.create(
                new SpAuditActionResultCreateDTO(
                        entity.getRybh(),
                        entity.getLcbh(),
                        "",
                        AuditBusinessTypeEnum.REVOKE_PROBATION,
                        AllAuditActionStatusEnum.CREATE,
                        user.getUsername(),
                        entity.getTenantId(),
                        user.getDeptId()
                ));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SpRevokeProbationUpdateDTO dto) {
        SpRevokeProbationEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        //保存主体信息
        super.updateById(SpRevokeProbationConvert.INSTANCE.update(dto));
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    @Override
    public void changeAction(SpComplexPreActionUpdateDTO dto) {
        SpRevokeProbationEntity entity = Optional.ofNullable(this.getOne(wrapper(new SpRevokeProbationQueryDTO(dto.getRybh(), dto.getLcbh()))))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        entity.setPreAction(dto.getAction());
        super.updateById(entity);
    }

    public void saveInstrumentList(SpRevokeProbationCreateDTO dto, SpRevokeProbationEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_REVOKE_PROBATION), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(SpRevokeProbationUpdateDTO dto, SpRevokeProbationEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_REVOKE_PROBATION), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(SpRevokeProbationResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_REVOKE_PROBATION);
        dto.setInstrumentList(fileService.findList(query));
    }

    private LambdaQueryWrapper<SpRevokeProbationEntity> wrapper(SpRevokeProbationQueryDTO query) {
        LambdaQueryWrapper<SpRevokeProbationEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(SpRevokeProbationEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(SpRevokeProbationEntity::getRybh, v));
        Queries.accept(query.getLcbh(), v -> wrapper.eq(SpRevokeProbationEntity::getLcbh, v));
        Queries.accept(query.getPreAction(), v -> wrapper.eq(SpRevokeProbationEntity::getPreAction, v));
        return wrapper;
    }
}